import { Http, Response, Headers } from '@angular/http';
import 'rxjs/add/operator/toPromise';
declare const $: any;
export class BaseService {
    protected http: Http;
    get Host() {
        return "http://yuxun.cn-szyt.cn:8080/yxItem/";
    }
    set Token(value: string) {
        localStorage.setItem("token", value);
    }
    get Token() {
        return localStorage.getItem("token");
    }

    Get(url: string, params?: any) {
        let u = this.Host + url;
        if (params) {
            let p = [];
            u = u + "?";
            for (let key in params) {
                p.push(key + "=" + params[key]);
            }
            u = u + p.join("&");
        }

        let result = (this.http.get(url) as any).toPromise();
        return this.ResultCallback(result);
    }

    Post<T>(url: string, params?: any, header?: Headers) {
        url = this.Host + url;
        let headers;
        if (header) {
            headers = header;
        }
        else {
            headers = new Headers();
            headers.append("Content-Type", "application/json");
            if (this.Token && params) {
                params.token = this.Token;
            }
        }
        let result = (<any>this.http.post(url, JSON.stringify(params), { headers: headers })).toPromise();
        return this.ResultCallback<T>(result);
    }
    Put<T>(url: string, params?: any) {
        url = this.Host + url;
        let headers = new Headers();
        headers.append("Content-Type", "application/json");
        if (this.Token && params) {
            params.token = this.Token;
        }
        let result = (<any>this.http.put(url, JSON.stringify(params), { headers: headers })).toPromise();
        return this.ResultCallback<T>(result);
    }

    UploadFile(file: any) {
        var fd = new FormData();
        let token = this.Token;
        let hostName = "http://yuxun.oss-cn-shenzhen.aliyuncs.com/";
        fd.append("file", file);
        fd.append("token", token);

        return new Promise<any>((resolve, reject) => {
            $.ajax({
                type: 'post',
                url: this.Host + "manager/upload_new_images",
                data: fd,
                processData: false,
                contentType: false,
                success: function (data: any) {
                    console.log(data);
                    if (data.code == 200) {
                        resolve(hostName + data.result);
                    }
                    else reject(data.message);
                },
                error: function (errMsg: any) {
                    reject(errMsg);
                },
            });
        })

    }

    private ResultCallback<T>(result: any): Promise<T> {
        return new Promise((resolve, reject) => {
            result.then((data: any) => {
                if (data.ok) {
                    let rData = data.json();
                    if (rData.code != 200) {
                        reject(rData.message);
                    }
                    resolve(rData.result);
                }
                else reject(data.json().message);
            }).catch((err: any) => {
                console.log(err);
                reject(err);
            })
        });
    }

}